<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Service\Attraction_info;

use Plugin\Attractions\AttractionManagement\Repository\Attraction_info\AttractionInfoRepository;
use Plugin\Attractions\AttractionManagement\Repository\Attraction_spot\AttractionSpotRepository;
use Plugin\Attractions\AttractionManagement\Repository\Attraction_type\AttractionTypeRepository;
use App\Service\IService;

/**
 * 信息资讯表服务类
 */
final class AttractionInfoService extends IService
{
    public function __construct(
        protected readonly AttractionInfoRepository $repository,
        protected readonly AttractionSpotRepository $spotRepository,
        protected readonly AttractionTypeRepository $typeRepository
    ) {}

    /**
     * 重写page方法，添加点位名称关联
     */
    public function page(array $params, int $page = 1, int $pageSize = 10): array
    {
        $result = $this->repository->page($params, $page, $pageSize);
        
        if (isset($result['list']) && is_array($result['list'])) {
            foreach ($result['list'] as &$item) {
                // 获取点位名称
                if (!empty($item['spot_id'])) {
                    $spot = $this->spotRepository->findById($item['spot_id']);
                    if ($spot) {
                        $item['spot_name'] = $spot->name ?? '未知点位名称';
                    } else {
                        $item['spot_name'] = '点位不存在';
                    }
                } else {
                    $item['spot_name'] = '未选择';
                }
                
                // 获取类型名称
                if (!empty($item['type_id'])) {
                    $type = $this->typeRepository->findById($item['type_id']);
                    if ($type) {
                        $item['type_name'] = $type->name ?? '未知类型';
                    } else {
                        $item['type_name'] = '类型不存在';
                    }
                } else {
                    $item['type_name'] = '未选择';
                }
            }
        }
        
        return $result;
    }
}